<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>npm发包 | hishion blog</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/blog/img/favicon.ico">
    <meta name="description" content="web front-end development engineer">
    <meta name="title" content="hishion blog">
    <meta name="keywords" content="小程序,Vue,uni-app,LESS/SCSS,Git">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/blog/assets/css/0.styles.85454e8d.css" as="style"><link rel="preload" href="/blog/assets/js/app.17d93a93.js" as="script"><link rel="preload" href="/blog/assets/js/2.d5810a70.js" as="script"><link rel="preload" href="/blog/assets/js/4.20ff38eb.js" as="script"><link rel="prefetch" href="/blog/assets/js/10.a2c8b9d7.js"><link rel="prefetch" href="/blog/assets/js/11.6316d0fd.js"><link rel="prefetch" href="/blog/assets/js/12.927e97fe.js"><link rel="prefetch" href="/blog/assets/js/13.5647b041.js"><link rel="prefetch" href="/blog/assets/js/14.2ae9363a.js"><link rel="prefetch" href="/blog/assets/js/15.00f0b12c.js"><link rel="prefetch" href="/blog/assets/js/16.cf70fbc0.js"><link rel="prefetch" href="/blog/assets/js/17.03f65a2a.js"><link rel="prefetch" href="/blog/assets/js/18.eab9c888.js"><link rel="prefetch" href="/blog/assets/js/19.41e2fb9c.js"><link rel="prefetch" href="/blog/assets/js/20.a81ff0d7.js"><link rel="prefetch" href="/blog/assets/js/21.8f4e6ef9.js"><link rel="prefetch" href="/blog/assets/js/22.33273307.js"><link rel="prefetch" href="/blog/assets/js/23.b9ccad42.js"><link rel="prefetch" href="/blog/assets/js/24.925b6f8f.js"><link rel="prefetch" href="/blog/assets/js/25.09121221.js"><link rel="prefetch" href="/blog/assets/js/26.93b4179f.js"><link rel="prefetch" href="/blog/assets/js/27.248627a6.js"><link rel="prefetch" href="/blog/assets/js/28.a76943a2.js"><link rel="prefetch" href="/blog/assets/js/29.56872daa.js"><link rel="prefetch" href="/blog/assets/js/3.c69e6cb7.js"><link rel="prefetch" href="/blog/assets/js/30.175b2658.js"><link rel="prefetch" href="/blog/assets/js/31.d027670c.js"><link rel="prefetch" href="/blog/assets/js/32.040794e0.js"><link rel="prefetch" href="/blog/assets/js/33.dd9eea2e.js"><link rel="prefetch" href="/blog/assets/js/34.2b31e291.js"><link rel="prefetch" href="/blog/assets/js/35.009bbce8.js"><link rel="prefetch" href="/blog/assets/js/36.ecb36989.js"><link rel="prefetch" href="/blog/assets/js/37.282a8534.js"><link rel="prefetch" href="/blog/assets/js/38.e29d954d.js"><link rel="prefetch" href="/blog/assets/js/39.e071deb9.js"><link rel="prefetch" href="/blog/assets/js/40.e30e040e.js"><link rel="prefetch" href="/blog/assets/js/41.f9beaf4c.js"><link rel="prefetch" href="/blog/assets/js/42.631fa7df.js"><link rel="prefetch" href="/blog/assets/js/43.de052ada.js"><link rel="prefetch" href="/blog/assets/js/44.b9d90c8b.js"><link rel="prefetch" href="/blog/assets/js/45.9edc6da4.js"><link rel="prefetch" href="/blog/assets/js/46.43221d60.js"><link rel="prefetch" href="/blog/assets/js/47.70bd2046.js"><link rel="prefetch" href="/blog/assets/js/48.f2da250c.js"><link rel="prefetch" href="/blog/assets/js/49.db6466ad.js"><link rel="prefetch" href="/blog/assets/js/5.413bc4a0.js"><link rel="prefetch" href="/blog/assets/js/50.465c1401.js"><link rel="prefetch" href="/blog/assets/js/51.25711c0b.js"><link rel="prefetch" href="/blog/assets/js/52.c3dd4c01.js"><link rel="prefetch" href="/blog/assets/js/53.c398474b.js"><link rel="prefetch" href="/blog/assets/js/54.1ff939f2.js"><link rel="prefetch" href="/blog/assets/js/55.d00053e3.js"><link rel="prefetch" href="/blog/assets/js/56.4978060f.js"><link rel="prefetch" href="/blog/assets/js/57.b9b8c161.js"><link rel="prefetch" href="/blog/assets/js/58.d67ad12f.js"><link rel="prefetch" href="/blog/assets/js/59.3a1f358e.js"><link rel="prefetch" href="/blog/assets/js/6.747c84d7.js"><link rel="prefetch" href="/blog/assets/js/60.7b70acc9.js"><link rel="prefetch" href="/blog/assets/js/61.43c54fc8.js"><link rel="prefetch" href="/blog/assets/js/62.d27744a0.js"><link rel="prefetch" href="/blog/assets/js/63.73378bfc.js"><link rel="prefetch" href="/blog/assets/js/64.060c996e.js"><link rel="prefetch" href="/blog/assets/js/65.747c9a84.js"><link rel="prefetch" href="/blog/assets/js/66.2a577ef9.js"><link rel="prefetch" href="/blog/assets/js/67.94620d6a.js"><link rel="prefetch" href="/blog/assets/js/68.3cf29532.js"><link rel="prefetch" href="/blog/assets/js/69.9e992128.js"><link rel="prefetch" href="/blog/assets/js/7.7a113c37.js"><link rel="prefetch" href="/blog/assets/js/70.0bf5d79a.js"><link rel="prefetch" href="/blog/assets/js/71.3c940d64.js"><link rel="prefetch" href="/blog/assets/js/72.b39fd551.js"><link rel="prefetch" href="/blog/assets/js/73.b22cb822.js"><link rel="prefetch" href="/blog/assets/js/74.f06bc4d1.js"><link rel="prefetch" href="/blog/assets/js/75.b7bdb0f0.js"><link rel="prefetch" href="/blog/assets/js/76.1dab014e.js"><link rel="prefetch" href="/blog/assets/js/77.7edba438.js"><link rel="prefetch" href="/blog/assets/js/8.eb2c369b.js"><link rel="prefetch" href="/blog/assets/js/9.8d45fb33.js">
    <link rel="stylesheet" href="/blog/assets/css/0.styles.85454e8d.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/blog/" class="home-link router-link-active"><!----> <span class="site-name">hishion blog</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/blog/" class="nav-link">
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="文章" class="dropdown-title"><span class="title">文章</span> <span class="arrow down"></span></button> <button type="button" aria-label="文章" class="mobile-dropdown-title"><span class="title">文章</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/basic/" class="nav-link">
  前端基础
</a></li><li class="dropdown-item"><!----> <a href="/blog/mp/" class="nav-link">
  小程序
</a></li><li class="dropdown-item"><!----> <a href="/blog/vue/" class="nav-link">
  Vue
</a></li><li class="dropdown-item"><!----> <a href="/blog/uni/life.html" class="nav-link">
  uni-app
</a></li><li class="dropdown-item"><!----> <a href="/blog/scss/" class="nav-link">
  SCSS/LESS
</a></li><li class="dropdown-item"><!----> <a href="/blog/git/" class="nav-link">
  Git
</a></li><li class="dropdown-item"><!----> <a href="/blog/standard/" class="nav-link">
  前端编码规范
</a></li><li class="dropdown-item"><!----> <a href="/blog/tool/" class="nav-link router-link-active">
  工具
</a></li><li class="dropdown-item"><!----> <a href="/blog/http/" class="nav-link">
  网络
</a></li><li class="dropdown-item"><!----> <a href="/blog/linux/" class="nav-link">
  Linux
</a></li><li class="dropdown-item"><!----> <a href="/blog/graphql/" class="nav-link">
  GraphQL
</a></li><li class="dropdown-item"><!----> <a href="/blog/other/" class="nav-link">
  其他
</a></li></ul></div></div><div class="nav-item"><a href="https://gitee.com/hishion.com" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://github.com/weixisheng" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/blog/" class="nav-link">
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="文章" class="dropdown-title"><span class="title">文章</span> <span class="arrow down"></span></button> <button type="button" aria-label="文章" class="mobile-dropdown-title"><span class="title">文章</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/basic/" class="nav-link">
  前端基础
</a></li><li class="dropdown-item"><!----> <a href="/blog/mp/" class="nav-link">
  小程序
</a></li><li class="dropdown-item"><!----> <a href="/blog/vue/" class="nav-link">
  Vue
</a></li><li class="dropdown-item"><!----> <a href="/blog/uni/life.html" class="nav-link">
  uni-app
</a></li><li class="dropdown-item"><!----> <a href="/blog/scss/" class="nav-link">
  SCSS/LESS
</a></li><li class="dropdown-item"><!----> <a href="/blog/git/" class="nav-link">
  Git
</a></li><li class="dropdown-item"><!----> <a href="/blog/standard/" class="nav-link">
  前端编码规范
</a></li><li class="dropdown-item"><!----> <a href="/blog/tool/" class="nav-link router-link-active">
  工具
</a></li><li class="dropdown-item"><!----> <a href="/blog/http/" class="nav-link">
  网络
</a></li><li class="dropdown-item"><!----> <a href="/blog/linux/" class="nav-link">
  Linux
</a></li><li class="dropdown-item"><!----> <a href="/blog/graphql/" class="nav-link">
  GraphQL
</a></li><li class="dropdown-item"><!----> <a href="/blog/other/" class="nav-link">
  其他
</a></li></ul></div></div><div class="nav-item"><a href="https://gitee.com/hishion.com" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://github.com/weixisheng" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前端基础</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>小程序</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Vue</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>SCSS/LESS</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Git知识</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前端规范</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>工具</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/blog/tool/" aria-current="page" class="sidebar-link">markdown语法</a></li><li><a href="/blog/tool/vscode.html" class="sidebar-link">VSCode</a></li><li><a href="/blog/tool/hbuilder.html" class="sidebar-link">HBuilder</a></li><li><a href="/blog/tool/extensions.html" class="sidebar-link">扩展程序</a></li><li><a href="/blog/tool/cmder.html" class="sidebar-link">Cmder</a></li><li><a href="/blog/tool/mirror.html" class="sidebar-link">镜像</a></li><li><a href="/blog/tool/npm.html" aria-current="page" class="active sidebar-link">npm发包</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/tool/npm.html#起步" class="sidebar-link">起步</a></li><li class="sidebar-sub-header"><a href="/blog/tool/npm.html#调试" class="sidebar-link">调试</a></li><li class="sidebar-sub-header"><a href="/blog/tool/npm.html#账号" class="sidebar-link">账号</a></li><li class="sidebar-sub-header"><a href="/blog/tool/npm.html#发布" class="sidebar-link">发布</a></li><li class="sidebar-sub-header"><a href="/blog/tool/npm.html#管理" class="sidebar-link">管理</a></li></ul></li><li><a href="/blog/tool/cli.html" class="sidebar-link">自定义脚手架</a></li><li><a href="/blog/tool/iconfont.html" class="sidebar-link">字体文件转base64</a></li><li><a href="/blog/tool/autocmd.html" class="sidebar-link">自动cmd命令</a></li><li><a href="/blog/tool/yarn.html" class="sidebar-link">yarn命令</a></li><li><a href="/blog/tool/package-version.html" class="sidebar-link">package version说明</a></li><li><a href="/blog/tool/debug.html" class="sidebar-link">移动端调试工具</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Linux知识</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>GraphQL</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>网络知识</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>其他</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="npm发包"><a href="#npm发包" class="header-anchor">#</a> npm发包</h1> <p>我们在开发的过程中都会从 <code>npm</code> 上下载包，来帮助我们提高工作效率。如果把自己的一些方法封装成包，并发布，后续自己安装就方便了。</p> <p>此文主要记录最近发包到 <code>npm</code> 的过程。<a href="https://github.com/weixisheng/uniapp-utils" target="_blank" rel="noopener noreferrer">@hishion_wei/uniapp-utils<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h2 id="起步"><a href="#起步" class="header-anchor">#</a> 起步</h2> <h3 id="npm基础知识"><a href="#npm基础知识" class="header-anchor">#</a> npm基础知识</h3> <ul><li>install</li></ul> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token function">npm</span> <span class="token function">install</span> <span class="token operator">&lt;</span>packge<span class="token operator">&gt;</span> <span class="token punctuation">[</span>--save-prod<span class="token operator">|</span>--save-dev<span class="token operator">|</span>--save-optional<span class="token operator">|</span>--no-save<span class="token punctuation">]</span> <span class="token punctuation">[</span>--save-exact<span class="token operator">|</span>--save-bundle<span class="token punctuation">]</span>
<span class="token comment">## -P, --save-prod，默认。依赖项会出现在 dependencies</span>
<span class="token comment">## -D, --save-dev。依赖项会出现在 devDependencies</span>
<span class="token comment">## -O, --save-optional。依赖项会出现在 optionalDependencies</span>
<span class="token comment">## --no-save。依赖项不会出现在 dependencies</span>

<span class="token comment">## -E, --save-exact。依赖项将配置一个确切的版本</span>
<span class="token comment">## -B, --save-bundle。依赖项同时被添加到 bundleDependencies</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><ul><li>link
一般是两步操作，之后就可以在其他项目方便调试了：</li></ul> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment">## 1. 在需要发布的包npm link创建一个全局映射</span>
<span class="token builtin class-name">cd</span> ~/projects/my-package
<span class="token function">npm</span> <span class="token function">link</span>
<span class="token comment">## 2. 在需要使用包的地方npm link &lt;packge-name&gt;</span>
<span class="token comment">## &lt;package-name&gt;是package.json中的name，不是目录名</span>
<span class="token builtin class-name">cd</span> ~/projects/other-project
<span class="token function">npm</span> <span class="token function">link</span> mypackage
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h3 id="初始化"><a href="#初始化" class="header-anchor">#</a> 初始化</h3> <p>首先得有一个本地开发的项目用来发布，可以创建一个 新目录使用 <code>npm init</code> 初始化项目信息。
<img src="/blog/assets/img/npm-1.7de9520f.png" alt="npm init"></p> <p>其中<code>entry</code>是入口文件，可以根据项目实际情况填写其他路径，例如：<code>lib.index.js</code>。</p> <p>然后添加一些文件，这里添加一个<code>src/index.js</code>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 内容自定义</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token keyword">function</span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token parameter">a<span class="token punctuation">,</span> b</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> a <span class="token operator">+</span> b
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h2 id="调试"><a href="#调试" class="header-anchor">#</a> 调试</h2> <p><code>npm</code> 包在发布之前，其他项目还无法直接安装使用。这时候可以使用 <code>npm link</code>关联。</p> <p>进入<code>npm-test</code>目录，执行<code>npm link</code>关联到全局，相当于<code>npm install npm-test -g</code>，只不过此时指向的是本地库。</p> <p><img src="" alt="npm link"></p> <p>然后找一个需要用到这个包的项目 <code>my-project</code>，执行：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token function">npm</span> <span class="token function">link</span> npm-test
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>从node_modules下面能看出 <code>npm-test</code> 与其他<code>npm</code> 包有些许不同，前面有个链接符号。</p> <p><img src="" alt="npm module link"></p> <p>准备好了之后，引用这个包，像其他包一样使用其方法，可以看到控制台输出3。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">import</span> add <span class="token keyword">from</span> <span class="token string">'npm-test'</span>

<span class="token keyword">let</span> sum <span class="token operator">=</span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>sum<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>说明包里面的方法正常使用了，还可以根据情况转义es6代码为es5。</p> <p>经过开发调试阶段，确认正常之后，现在可以在 <code>my-project</code> 取消关联，并准备发布到 <code>npm</code> 了。</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token function">npm</span> unlink npm-test
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="账号"><a href="#账号" class="header-anchor">#</a> 账号</h2> <ul><li>注册</li></ul> <p>去 <a href="https://www.npmjs.com/" target="_blank" rel="noopener noreferrer">npm<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 注册一个账号，在发布包的时候需要验证账号权限。</p> <p>或者使用 <code>npm adduser</code> 添加，为了防止设置了其他镜像，先重置镜像为 <code>npmjs</code></p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token function">npm</span> config <span class="token builtin class-name">set</span> registry https://registry.npmjs.org

<span class="token function">npm</span> adduser
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><ul><li>登录</li></ul> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token function">npm</span> login
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><img src="" alt="npm login"></p> <h2 id="发布"><a href="#发布" class="header-anchor">#</a> 发布</h2> <p>经过前面的准备阶段，现在可以准备发布包了。有几方面需要注意下</p> <h3 id="文件管理"><a href="#文件管理" class="header-anchor">#</a> 文件管理</h3> <ul><li>使用files</li></ul> <p>参考 <code>element-ui</code> 的 <code>package.json</code>，发现里面使用的是 <code>files</code> 选项来控制需要发布的文件。<strong>根据实际开发包选择使用</strong>。</p> <div class="language-json line-numbers-mode"><pre class="language-json"><code> <span class="token property">&quot;main&quot;</span><span class="token operator">:</span> <span class="token string">&quot;lib/element-ui.common.js&quot;</span><span class="token punctuation">,</span>
 <span class="token property">&quot;files&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>
   <span class="token string">&quot;lib&quot;</span><span class="token punctuation">,</span>
   <span class="token string">&quot;src&quot;</span><span class="token punctuation">,</span>
   <span class="token string">&quot;packages&quot;</span><span class="token punctuation">,</span>
   <span class="token string">&quot;types&quot;</span>
 <span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><ul><li>使用ignore</li></ul> <p>在根目录创建 <code>.npmignore</code> 文件，类似 <code>.gitignore</code>，如果没有<code>.npmignore</code> 会使用 <code>.gitignore</code> 替代。例如：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># 不发布src源码</span>
src
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>默认发布的文件</li></ul> <p>无法通过 <code>.npmignore</code> 和 <code>.gitignore</code> 设置：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>package.json
README
CHANGELOG
LICENSE/LICENCE
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><ul><li>默认忽略的文件</li></ul> <p>不需要显式添加到 <code>.npmignore</code> ：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>.*.swp
._*
.DS_Store
.git
.hg
.npmrc
.lock-wscript
.svn
.wafpickle-*
config.gypi
CVS
npm-debug.log
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p><code>node_modules</code> 也是忽略的，一些依赖文件npm会自动处理，无需加到 <code>.npmignore</code>。</p> <h3 id="版本管理"><a href="#版本管理" class="header-anchor">#</a> 版本管理</h3> <div class="custom-block tip"><p class="custom-block-title">standard-version</p> <p>使用 <a href="https://github.com/conventional-changelog/standard-version" target="_blank" rel="noopener noreferrer">standard-version<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 生成版本更新和日志</p></div> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token function">npm</span> i standard-version -D
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>然后在 <code>package.json</code> 添加一个<code>script</code>，后续升级执行 <code>npm run release</code> 即可。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">&quot;scripts&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
 <span class="token comment">//  &quot;release&quot;: &quot;standard-version&quot;</span>
 <span class="token string">&quot;release&quot;</span><span class="token operator">:</span> <span class="token string">&quot;standard-version &amp;&amp; git push --follow-tags origin master &amp;&amp; npm publish&quot;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>如果git提交信息符合规范，就会自动生成日志
参考 <a href="/blog/git/#基本格式">Git知识&gt;Git提交规范&gt;基本格式</a>。例如：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>feat: 初始化
fix: 修复问题
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>初次发布可以这样写 <code>npm run release -- --first-release</code></p> <p><img src="/blog/assets/img/npm-6.f7259a4d.png" alt="npm publish"></p> <h3 id="发布-2"><a href="#发布-2" class="header-anchor">#</a> 发布</h3> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token function">npm</span> publish
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>集成在命令 <code>npm run release</code> 中。</p> <p>发布成功之后，可以在 <a href="https://www.npmjs.com/" target="_blank" rel="noopener noreferrer">npm<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 上搜索，类似：</p> <p><img src="/blog/assets/img/npm-5.4dacf8d4.png" alt="npm package"></p> <h2 id="管理"><a href="#管理" class="header-anchor">#</a> 管理</h2> <ul><li>发布scope包</li></ul> <p>如果发布 <code>scope</code> 包，需要修改 <code>package.json</code> 的 <code>name</code> 为包含组织的包名，格式为：<code>@somescope/somepackagename</code>，例如 <code>@vue/cli-service</code></p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token function">npm</span> publish --access public
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>删除包</li></ul> <p>使用 <code>npm unpublish</code> 命令删除某个包或者包的某个版本。</p> <p>删除某个版本之后，这个版本就不再可用，必须使用新版本号；删除整个包需要24小时后才能发布新版本</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># 删除包的某个版本</span>
<span class="token function">npm</span> unpublish <span class="token punctuation">[</span><span class="token operator">&lt;</span>@scope<span class="token operator">&gt;</span>/<span class="token punctuation">]</span><span class="token operator">&lt;</span>pkg<span class="token operator">&gt;</span>@<span class="token operator">&lt;</span>version<span class="token operator">&gt;</span>
<span class="token comment"># 删除全部</span>
<span class="token function">npm</span> unpublish <span class="token punctuation">[</span><span class="token operator">&lt;</span>@scope<span class="token operator">&gt;</span>/<span class="token punctuation">]</span><span class="token operator">&lt;</span>pkg<span class="token operator">&gt;</span> --force
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><ul><li>废弃包</li></ul> <p>如果不用维护某个包，或者提醒使用者更新版本，可以标记某个包或者某个版本为废弃的</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token function">npm</span> deprecate <span class="token operator">&lt;</span>pkg<span class="token operator">&gt;</span><span class="token punctuation">[</span>@<span class="token operator">&lt;</span>version range<span class="token operator">&gt;</span><span class="token punctuation">]</span> <span class="token operator">&lt;</span>message<span class="token operator">&gt;</span>

<span class="token comment">## example</span>
<span class="token function">npm</span> deprecate my-thing@<span class="token string">&quot;&lt; 0.2.3&quot;</span> <span class="token string">&quot;critical bug fixed in v0.2.3&quot;</span>
<span class="token function">npm</span> deprecate my-thing@1.x <span class="token string">&quot;1.x is no longer supported&quot;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/blog/tool/mirror.html" class="prev">
        镜像
      </a></span> <span class="next"><a href="/blog/tool/cli.html">
        自定义脚手架
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="/blog/assets/js/app.17d93a93.js" defer></script><script src="/blog/assets/js/2.d5810a70.js" defer></script><script src="/blog/assets/js/4.20ff38eb.js" defer></script>
  </body>
</html>
